package com.eascs.afterloancheck.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.afterloancheck.entity.CheckLoanEntity;
import com.eascs.afterloancheck.form.CheckLoanQueryForm;
import com.eascs.afterloancheck.service.LoanCheckServicce;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.file.FileTypeConstants;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.tag.MenuTag;

@Controller
@RequestMapping("/afterloancheck")
public class AfterLoanCheckController extends BaseMasterViewController {

	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;

	@Autowired
	LoanCheckServicce loanCheckServicce;

	@RequestMapping("/index")
	public String index(CheckLoanQueryForm form) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "AfterLoanCheck");
		String checkurl = "/rafterloancheck/list";
		if (form != null) {
			checkurl = form.getBillCode() == null ? UrlUtil.appendUrl(checkurl, "billCode", "")
					: UrlUtil.appendUrl(checkurl, "billCode", form.getBillCode());
			checkurl = form.getLoanUserName() == null ? UrlUtil.appendUrl(checkurl, "loanUserName", "")
					: UrlUtil.appendUrl(checkurl, "loanUserName", form.getLoanUserName());
			checkurl = form.getOrgManager() == null ? UrlUtil.appendUrl(checkurl, "orgManager", "")
					: UrlUtil.appendUrl(checkurl, "orgManager", form.getOrgManager());
			checkurl = form.getRevisiterName() == null ? UrlUtil.appendUrl(checkurl, "revisiterName", "")
					: UrlUtil.appendUrl(checkurl, "revisiterName", form.getRevisiterName());
			checkurl = checkurl + "&pageIndex=";
		} else {
			checkurl = checkurl + "?pageIndex=";
		}
		form.setPageSize(10);
		ViewContext.put("checkurl", checkurl);
		ViewContext.put("form", form);
		return "/afterloancheck/index";
	}

	@RequestMapping("/to_model")
	public String toModel(String parameter) {
		CheckLoanEntity vo = loanCheckServicce.queryEntityByReferId(parameter);
		queryDataList(vo.getId(), FileTypeConstants.LOAN_CHECK_TYPE_RPMENT, null);
		ViewContext.put("vo", vo);
		return "/afterloancheck/detail_model";
	}

	/**
	 * 贷后详情-贷后检查详情
	 * 
	 * @param billCode
	 * @return
	 */
	@RequestMapping("/listEntitys")
	public String toList(String billCode) {
		PageResult<Object[]> paymentsResult = loanCheckServicce.listCheckLoanEntity(billCode);
		String[] checkListstitles = { "回访单号", "流程提交人", "回访人", "回访日期", "回访形式", "回访类型", "月均销售额", "新增负债额", "流程状态" };
		SelectDataOption[] selectDataOptions = { null, null, null, null, new SelectDataOption("REVISITFORM"),
				new SelectDataOption("REVISITTYPE"), null, null, new SelectDataOption("parWFStutus") };
		Button[] Button = new Button[] { ButtonHelper.createAjaxButton(null, "/rafterloancheck/view_detail?id="),
				null };
		PaginationInfo checkListInfo = new PaginationInfo(paymentsResult, checkListstitles);
		checkListInfo.setSelectDataOptions(selectDataOptions);
		checkListInfo.setBuildPagingNavigation(false);
		checkListInfo.setFirstRenderColumnIndex(1);
		checkListInfo.setBuildRowNumber(true);
		checkListInfo.setTitleButtons(Button);
		ViewContext.put("checkListInfo", checkListInfo);
		return "/afterloancheck/check_list";
	}

	public void queryDataList(String loanapplyNo, String code, String detailFlag) {
		PageResult<Object[]> pageResult = o2oLoanapplyService.findO2oDataListEntityById(loanapplyNo, "'" + code + "'");
		String titles[] = { "附件类型", "文件名", "提交人", "提交时间", "更新时间" };
		Button[] titleButtons = new Button[] { null,
				ButtonHelper.createOpenButton(null, "/file/download?datalistno=") };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
		paginationInfo.setTitleButtons(titleButtons);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setBuildRowNumber(true);
		ViewContext.put("pageResult", paginationInfo);// 个人附件
	}

}
